//
//  HOReplyListViewController.m
//  微度报修
//
//  Created by foguo-mac-1 on 7/3/16.
//  Copyright © 2016 WeiDu. All rights reserved.
//

#import "HOReplyListViewController.h"
#import "HOConstants.h"
#import "UIImageView+WebCache.h"
#import "HOCommentEntity.h"
#import "HOImageMgr.h"
#import "HOUtils.h"
#import "HOCommentTableViewCell.h"
#import "HOUploadImageView.h"
#import "HORecordView.h"
#import "HOBigImageViewController.h"

@interface HOReplyListViewController ()<HOUploadImageViewDelegate>

@property(nonatomic, assign)CGFloat singleImgWidth;
@end

@implementation HOReplyListViewController


- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view.
	self.singleImgWidth = (self.view.frame.size.width - 4 * HO_HORIZONAL_PADDING) / 3;
	[self initTableViewWithPullUp];
	[self requestDataWithMaxId:1];
	
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	[self setNavigationItemTitle:NSLocalizedString(@"历史回复(流转记录)", @"历史回复(流转记录)")];
	
}

- (void)requestDataWithMaxId:(int)maxId
{
	if(self.isExtraAssistant)
	{
		[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_GetExternalCommentsList,
									  kHO_FIELD_MaxID:[NSNumber numberWithInt:maxId],
									  kHO_FIELD_OrderId:[NSNumber numberWithInt:self.orderId]}];
	}
	else
	{
		[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_GetCommentsList,
									  kHO_FIELD_MaxID:[NSNumber numberWithInt:maxId],
									  kHO_FIELD_OrderId:[NSNumber numberWithInt:self.orderId]}];
	}
}

- (void)pullUpToRefresh
{
	HOCommentEntity *entity = [self.tableViewDataList lastObject];
	if(entity)
	{
		[self requestDataWithMaxId:entity.commentId];
	}
	else
	{
		[self requestDataWithMaxId:1];
	}
}


#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	HOCommentEntity *commentEntity = [self.tableViewDataList objectAtIndex:indexPath.section];

	if(indexPath.row == 0)
	{
		HOCommentTableViewCell *cell = (HOCommentTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
		return [cell getCellHeight];
	}
	else
	{
		if([commentEntity.imageList count] == 0)
			return 0;
		return self.singleImgWidth + HO_HORIZONAL_PADDING * 2;
	}
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	NSInteger count = [self.tableViewDataList count];
	[self updateNoResultView:count];;
	return count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return 2;
}


- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	return SECTION_PADDING / 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	HOCommentEntity *commentEntity = [self.tableViewDataList objectAtIndex:indexPath.section];
	if(indexPath.row == 0)
	{
		
		static NSString *HOCommentTableViewCellIdentifier = @"HOCommentTableViewCellIdentifier";
		HOCommentTableViewCell *cell = (HOCommentTableViewCell *)[tableView dequeueReusableCellWithIdentifier:HOCommentTableViewCellIdentifier];
		if(cell == nil)
		{
			cell = [[[HOCommentTableViewCell alloc]
					 initWithStyle:UITableViewCellStyleDefault
					 reuseIdentifier: HOCommentTableViewCellIdentifier] autorelease];
			[cell initWithWidth:tableView.frame.size.width];
			cell.isExtraAssistant = self.isExtraAssistant;
			
		}
		[cell setEntity:commentEntity];
		return cell;
	}
	else if(indexPath.row == 1)
	{
		static NSString *HOPictureTableViewCellIdentifier = @"HOPictureTableViewCellIdentifier";
		UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:HOPictureTableViewCellIdentifier];
		if(cell == nil)
		{
			cell = [[[UITableViewCell alloc]
					 initWithStyle:UITableViewCellStyleDefault
					 reuseIdentifier: HOPictureTableViewCellIdentifier] autorelease];
			cell.textLabel.font = [UIFont systemFontOfSize:15];
			cell.textLabel.textColor = TEXT_COLOR_181818;
			cell.accessoryType = UITableViewCellAccessoryNone;
			cell.selectionStyle = UITableViewCellSelectionStyleNone;
			[cell.contentView.layer setMasksToBounds:YES];
		}
		HOUploadImageView *uploadImgView = (HOUploadImageView *)[cell viewWithTag:1114];
		if(uploadImgView == nil)
		{
			uploadImgView = [[[HOUploadImageView alloc]
								   initWithFrame:CGRectMake(0,
															134,
															self.view.frame.size.width,
															self.singleImgWidth)] autorelease];
			[uploadImgView hideUploadButton];
			uploadImgView.delegate = self;
			[cell.contentView addSubview:uploadImgView];
			uploadImgView.frame = CGRectMake(0,
												  HO_HORIZONAL_PADDING,
												  tableView.frame.size.width,
												  self.singleImgWidth);
			uploadImgView.tag = 1114;
		}
		[uploadImgView setImagePaths:commentEntity.imageList];
//		uploadImgView.layer.borderWidth = 1;
		return cell;
		
	}
	return nil;
}

- (void)viewFullImage:(NSArray *)images withCurrentIndex:(int)index
{
	HOBigImageViewController *viewCtrl = [[HOBigImageViewController alloc] init];
	viewCtrl.imagesData = images;
	viewCtrl.currentIndex = index;
	//	[self.navigationController pushViewController:viewCtrl animated:YES];
	[self.navigationController presentViewController:viewCtrl animated:YES completion:nil];
	[viewCtrl release];
	
	//	[self.navigationController pushViewController:viewCtrl animated:YES];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	
}
#pragma mark - -- tool
- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}


- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_GetCommentsList]
	   || [response isEqualAction:kHO_ACTION_GetExternalCommentsList])
	{
		for(int i = 0; i < [response.dataNode count]; i ++)
		{
			NSDictionary *dic = [response.dataNode objectAtIndex:i];
			HOCommentEntity *entity = [[HOCommentEntity alloc] init];
			[entity parseDic:dic];
			[self.tableViewDataList addObject:entity];
			[entity release];
		}
		[baseTableview reloadData];
	}
}
@end
